package cn.edu.neusoft.dnui.startproject;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


import cn.edu.neusoft.dnui.startproject.bean.WeatherInfo;
import cn.edu.neusoft.dnui.startproject.listener.WeatherListener;
import cn.edu.neusoft.dnui.startproject.model.Weather51Model;
import cn.edu.neusoft.dnui.startproject.model.WeatherModel;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, WeatherListener
{
    private EditText cityNOInput;
    private TextView city;
    private TextView cityNO;
    private TextView temp;
    private TextView wd;
    private TextView ws;
    private TextView sd;
    private ProgressDialog loadingWeather;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cityNOInput = (EditText) findViewById(R.id.et_city_no);
        city = (TextView) findViewById(R.id.tv_city);
        cityNO = (TextView) findViewById(R.id.tv_city_no);
        temp = (TextView) findViewById(R.id.tv_temp);
        wd = (TextView) findViewById(R.id.tv_WD);
        ws = (TextView) findViewById(R.id.tv_WS);
        sd = (TextView) findViewById(R.id.tv_SD);
        loadingWeather = new ProgressDialog(this);
        loadingWeather.setTitle("searching");
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }
    @Override
    public void onClick(View v)
    {
        loadingWeather.show();
        switch (v.getId())
        {
            case R.id.button1:
                WeatherModel weatherModel = new WeatherModel();
                weatherModel.getWeather(cityNOInput.getText().toString(), this);
                break;
            case R.id.button2:
                Weather51Model weather51Model = new Weather51Model();
                weather51Model.getWeather(cityNOInput.getText().toString(), this);

                break;
            default:
                break;
        }
    }
    @Override
    public void onResponse(WeatherInfo weather)
    {
        loadingWeather.dismiss();
        if (weather != null)
        {
            city.setText(weather.getCity());
            cityNO.setText(weather.getCityid());
            temp.setText(weather.getTemp());
            wd.setText(weather.getWD());
            ws.setText(weather.getWS());
            sd.setText(weather.getSD());
        } else
            {
            city.setText("unknown");
        }

    }
    @Override
    public void onFail(String msg)
    {
        loadingWeather.dismiss();
        city.setText(msg);
    }
}
//create by LeoXu224